<template>
  <div class="app-container">
    <span>搜索内容：</span>
    <el-input style="width: 150px; padding: 10px" v-model="keyword" placeholder="请输入用户id"></el-input>
    <span> 活动类型： </span>
    <template>
      <el-select v-model="type" placeholder="请选择类型" style="margin: 10px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </template>
    <el-button type="primary" round @click="fetchData(page)">搜索</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="用户id" width="150">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column label="记录id" width="150">
        <template slot-scope="scope">
          {{ scope.row.recordId }}
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" width="150">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="活动类型" width="150">
        <template slot-scope="scope">
          {{ scope.row.lotteryType }}
        </template>
      </el-table-column>
      <el-table-column label="星球名称" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.starName }}
        </template>
      </el-table-column>
      <el-table-column label="奖品价值" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="活动次数" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.count }}
        </template>
      </el-table-column>
      <el-table-column label="消费金额" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.consumePrice }}
        </template>
      </el-table-column>

      <el-table-column label="盈亏" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price-scope.row.consumePrice }}
        </template>
      </el-table-column>

      <el-table-column label="盈亏率" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price/scope.row.consumePrice }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" round @click="dialogFormGiveGiftVisible = true; giveGiftDTO.userId = scope.row.userId">
            礼物补发
          </el-button>
          <el-button size="mini" type="primary" round @click="dialogFormGiftItemVisible = true;getLotteryItemList(scope.row.recordId)">
            查看奖励
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-dialog title="礼物补发" :visible.sync="dialogFormGiveGiftVisible">
      <el-form ref="form" :model="giveGiftDTO" label-width="80px">
        <el-form-item label="礼物类型">
          <template>
            <el-select v-model="giveGiftDTO.type" placeholder="请选择类型" style="margin: 10px">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="礼物id">
          <el-input v-model="giveGiftDTO.giftId" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormGiveGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleGiveLotteryGift">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="奖品列表" :visible.sync="dialogFormGiftItemVisible" width="76%">
      <el-table
        v-loading="listLoading"
        style="margin-top: 10px"
        :data="itemList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="ID" width="110">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>

        <el-table-column label="名称" width="150">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="图标" width="200" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.icon"
              :preview-src-list="[scope.row.icon]"
            />
          </template>
        </el-table-column>
        <el-table-column label="价格" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.price }}
          </template>
        </el-table-column>

        <el-table-column label="数量" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.count }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog title="编辑奖励" :visible.sync="dialogEditVisible" width="76%">
      <el-form ref="form" :model="giftDTO" label-width="80px">
        <el-form-item label="礼物名称">
          <el-input v-model="giftDTO.name" />
        </el-form-item>
        <el-form-item label="礼物价格">
          <el-input-number v-model="giftDTO.price" :min="1" :max="100000000" label="描述文字" />
        </el-form-item>

        <el-form-item label="是否上架">
          <el-switch
            v-model="giftDTO.status"
          />
        </el-form-item>
        <el-form-item label="礼物类型">
          <el-select v-model="giftDTO.type" placeholder="礼物类型" style="margin: 10px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="礼物图标">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :limit="1"

            :on-success="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="giftDTO.icon" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="礼物特效">
          <el-upload
            class="upload-demo"
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            :on-remove="handleRemove2"
            :on-success="handlePictureCardPreview2"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传 svga 文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="giftDTO.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCreateGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddGfit">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import { getLotteryList, deleteById, addGift, getCurrentLottery, giveLotteryGift, getLotteryItemList } from '@/api/party'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      options: [{
        value: '1',
        label: '欢乐转盘'
      }, {
        value: '2',
        label: '黄金转盘'
      }, {
        value: '3',
        label: '初级宝箱'
      }, {
        value: '4',
        label: '中级宝箱'
      }, {
        value: '5',
        label: '高级宝箱'
      }, {
        value: '6',
        label: '吃货大排档'
      }, {
        value: '7',
        label: '星际探险'
      }],
      itemList: [],
      fileList: [],
      list: null,
      listLoading: true,
      keyword: '',
      status: '-10',
      page: 1,
      size: 10,
      total: 0,
      dialogFormGiftItemVisible: false,
      dialogFormGiveGiftVisible: false,
      giveGiftDTO: {},
      giftDTO: {
        name: '',
        circleType: '0',
        sort: 0,
        status: false,
        icon: '',
        price: '',
        materialUrl: ''
      },
      currentLotterys: [],
      type: '1',
      dialogEditVisible: false
    }
  },
  created() {
    this.getCurrentLottery()
    this.fetchData(this.page)
  },
  methods: {
    handleGiveLotteryGift() {
      this.dialogFormGiveGiftVisible = false
      giveLotteryGift(this.giveGiftDTO).then(res => {
        this.$message({
          type: 'success',
          message: '创建成功!'
        })
      })
    },
    handlePictureCardPreview2(response, file) {
      this.giftDTO.materialUrl = response.data.url
      console.log(this.giftDTO.materialUrl)
    },
    handlePictureCardPreview(response, file) {
      this.giftDTO.icon = response.data.url
    },
    handleRemove2(file, fileList) {
      this.fileList = []
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    handleRemove(file) {
      this.giftDTO.icon = ''
      console.log(file)
    },
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    handleAddGfit() {
      this.dialogCreateGiftVisible = false
      this.fileList = []
      addGift(this.giftDTO).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '创建成功!'
        })
      })
    },
    getCurrentLottery() {
      getCurrentLottery().then(res => {
        this.currentLotterys = res.data
      })
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getLotteryList({ page: this.page, size: this.size, type: this.type, keyword: this.keyword }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    getLotteryItemList(val) {
      getLotteryItemList({ recordId: val }).then(res => {
        this.itemList = res.data
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该动态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
